<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Pause the CPU usage timer</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="luasandbox.loadstring.html">« LuaSandbox::loadString</a></li>
      <li style="float: right;"><a href="luasandbox.registerlibrary.html">LuaSandbox::registerLibrary »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.luasandbox.html">LuaSandbox</a></li>
    <li>Pause the CPU usage timer</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="luasandbox.pauseusagetimer" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">LuaSandbox::pauseUsageTimer</h1>
  <p class="verinfo">(PECL luasandbox &gt;= 1.4.0)</p><p class="refpurpose"><span class="refname">LuaSandbox::pauseUsageTimer</span> &mdash; <span class="dc-title">Pause the CPU usage timer</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-luasandbox.pauseusagetimer-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>LuaSandbox::pauseUsageTimer</strong></span>(): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   Pauses the CPU usage timer.
  </p>
  <p class="para">
   This only has effect when called from within a callback from Lua. When
   execution returns to Lua, the timer will be automatically unpaused. If
   a new call into Lua is made, the timer will be unpaused for the
   duration of that call.
  </p>
  <p class="para">
   If a PHP callback calls into Lua again with timer not paused, and then
   that Lua function calls into PHP again, the second PHP call will not be
   able to pause the timer. The logic is that even though the second PHP
   call would avoid counting the CPU usage against the limit, the first
   call still counts it.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-luasandbox.pauseusagetimer-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-luasandbox.pauseusagetimer-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a <span class="type">bool</span> indicating whether the timer is now paused.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-luasandbox.pauseusagetimer-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3438">
    <p><strong>示例 #1 Manipulating the usage timer</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;create&nbsp;a&nbsp;new&nbsp;LuaSandbox&nbsp;and&nbsp;set&nbsp;a&nbsp;CPU&nbsp;limit<br /></span><span style="color: #0000BB">$sandbox&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">LuaSandbox</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$sandbox</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setCPULimit</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">);<br /><br />function&nbsp;</span><span style="color: #0000BB">doWait</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$t&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$end&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">microtime</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">)&nbsp;+&nbsp;</span><span style="color: #0000BB">$t</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(&nbsp;</span><span style="color: #0000BB">microtime</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">)&nbsp;&lt;&nbsp;</span><span style="color: #0000BB">$end&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;waste&nbsp;CPU&nbsp;cycles<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Register&nbsp;a&nbsp;PHP&nbsp;callback<br /></span><span style="color: #0000BB">$sandbox</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerLibrary</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'php'</span><span style="color: #007700">,&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'test'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;function&nbsp;()&nbsp;use&nbsp;(&nbsp;</span><span style="color: #0000BB">$sandbox&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$sandbox</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pauseUsageTimer</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">doWait</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">5&nbsp;</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$sandbox</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">unpauseUsageTimer</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">doWait</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">0.1&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;},<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'test2'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;function&nbsp;()&nbsp;use&nbsp;(&nbsp;</span><span style="color: #0000BB">$sandbox&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$sandbox</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pauseUsageTimer</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$sandbox</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">unpauseUsageTimer</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">doWait</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">1.1&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />]&nbsp;);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;should&nbsp;not&nbsp;time&nbsp;out...\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sandbox</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loadString</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'php.test()'&nbsp;</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">call</span><span style="color: #007700">();<br /><br />echo&nbsp;</span><span style="color: #DD0000">"This&nbsp;should&nbsp;time&nbsp;out.\n"</span><span style="color: #007700">;<br />try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$sandbox</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loadString</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'php.test2()'&nbsp;</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">call</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"It&nbsp;did&nbsp;not?\n"</span><span style="color: #007700">;<br />}&nbsp;catch&nbsp;(&nbsp;</span><span style="color: #0000BB">LuaSandboxTimeoutError&nbsp;$ex&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"It&nbsp;did!&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$ex</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
This should not time out...
This should time out.
It did! The maximum execution time for this script was exceeded
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-luasandbox.pauseusagetimer-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="luasandbox.setcpulimit.html" class="methodname" rel="rdfs-seeAlso">LuaSandbox::setCPULimit()</a> - Set the CPU time limit for the Lua environment</span></li>
    <li class="member"><span class="methodname"><a href="luasandbox.unpauseusagetimer.html" class="methodname" rel="rdfs-seeAlso">LuaSandbox::unpauseUsageTimer()</a> - Unpause the timer paused by LuaSandbox::pauseUsageTimer</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>